package com.yc.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Demo {

    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(8080);
        System.out.println("服务器启动:8080");
        while(true){
            Socket socket = server.accept();
            System.out.println("========== begin ============");
            InputStream in = socket.getInputStream();
            byte[] bytes = new byte[1024*10];
            int count;
            count = in.read(bytes);
            System.out.println(new String(bytes,0,count));
            System.out.println("=========== end ===========");

            OutputStream out = socket.getOutputStream();
            out.write("HTTP/1.1 200 OK\n".getBytes());
            // 响应体的格式说明
            out.write("Content-Type: text/html;charset=utf-8\n".getBytes());
            // 空行
            out.write("\n".getBytes());
            // 响应体
            out.write("<h1>Hello world!</h1>".getBytes());
            socket.close();
        }
    }

}
